﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.Animations;
using System.IO;
using System.Reflection;
using Silvermoon.Images;
using Silvermoon.Visuals;
using Silvermoon.OpenGL;
using DialogResult=Silvermoon.Controls.Classes.DialogResult;
using PictureBox=Silvermoon.Controls.PictureBox;

namespace FileExplorer
{

    public class MainMenu : Window
    {
        private Calculator calculator;
        private GridWindow gridWindow;
        private PictureBox box;
        private LensView view;
        private ExplorerWindow explorer;
        private StartMenuItem dateItem;
        int seconds;

        public MainMenu()
            : base()
        {
            // this windows does not need a toolbar:
            ShowToolbar = false;

            Texture b1 = new Texture();
            Stream str = Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileLife.Images.RegEdit.png");
            b1.LoadFromStream(str);

            // Get the background image:
            box = new PictureBox { Margin = new Margin(0) };
            box.Image = new Texture();
            SetBackground("Space.jpg");
            Controls.Add(box);

            int height = StartMenuItem.ItemHeight;
            view = new LensView { Margin = new Margin(0), TextColor = Color.White, LensHeight = 160, ItemHeight = height, Locked = false, KeepLensCentered = false };
            TextColor = Color.White;
            Controls.Add(view);
            
            StartMenuItem smi = new StartMenuItem("Mobile Life"
                , new StartMenuDetailChunk { Title = "Mobile Life", Details = "V" + this.GetType().Assembly.GetName().Version.ToString() + " Melectric 2009" }
                //,new StartMenuDetailChunk { Title = "Settings", Details = "Your Preferences" }
            );
            view.Items.Add(smi);


            smi = new StartMenuItem("Weight"
                , new StartMenuDetailChunk { Title = "Weight - Add", Details = "Add Record to log" }
                
            );
            smi.Chunks[0].Click += (s, e) => AddWeight();
            view.Items.Add(smi);



            smi = new StartMenuItem("Exit", "Exits the application.", "MobileLife.Images.Exit.png");
            smi.Chunks[0].Click += (s, e) => ExitApp();
            view.Items.Add(smi);


            // set the lens and the offset to a nice position:
            view.TopOffset = 0;
            view.LensOffset = 0;

        }



        /// <summary>
        /// Sets the background image.
        /// </summary>
        /// <param name="name">The name of the image to get from resource.</param>
        private void SetBackground(string name)
        {
            string resourcePath = string.Format("MobileLife.Images.{0}", name);
            Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(resourcePath);
            if (stream == null) throw new SystemException("Resource not found: " + resourcePath);
            box.Image.LoadFromStream(stream);

        }

        /// <summary>
        /// Method when an app closes
        /// </summary>
        void OnExplorerHiding(object sender, EventArgs e)
        {
            this.Visible = true;
            this.SendToBack();
            PreLoad();
        }

        /// <summary>
        /// Open Add Wieght
        /// </summary>
        private void AddWeight()
        {
            if (gridWindow == null)
            {
                gridWindow = new GridWindow();
                gridWindow.Hiding += OnExplorerHiding;
                gridWindow.Shown += (s, e) => { this.Visible = false; };
            }
            gridWindow.Show(TransitionMode.ZoomIn);
            gridWindow.Focus();



            //ConfirmDialog dlg = new ConfirmDialog("Add Weight", "Not Yet Implemented", true);
            
            //dlg.Margin = new Margin(0, null, 0, 0);
            //dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            //dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
            //{
                
            //});
        }

        /// <summary>
        /// To ensure to exit this application we show a dialog to confirm:
        /// </summary>
        private void ExitApp()
        {
            ConfirmDialog dlg = new ConfirmDialog("Exit Application", "Do you really want to quit?");
            dlg.Margin = new Margin(0, null, 0, 0);
            dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
                {
                    if (e.Result == DialogResult.OK) Screen.Close();
                });
        }


        public override void Focus()
        {
            view.Focus();
        }

    }
}
